package com.authine.cloudpivot.ext.applicationservice.api;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.authine.cloudpivot.ext.applicationservice.base.ApplicationBaseService;
import com.authine.cloudpivot.ext.model.order.OrderTraceVO;
import com.authine.cloudpivot.ext.service.YunShuService;
import com.authine.cloudpivot.ext.service.impl.YunShuServiceImpl;
import com.authine.cloudpivot.ext.utils.system.SessionUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.ArrayList;
import java.util.List;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

@Path("OrderTraceService")
@Slf4j
public class OrderTraceService  extends ApplicationBaseService {
    private YunShuService yunShuService = new YunShuServiceImpl();

    /**
     * 手动触发往牧原进销存接口推送数据
     *
     * @param orderTraceVO 发货记录
     * @return 结果
     */
    @Path("tempYunShuPush")
    @POST
    @Operation(summary = "手动触发往牧原进销存接口推送数据", tags = CUSTOM_SERVICE, description = "id必填,多个值用逗号隔开")
    public SingleResponse tempYunShuPush(@Parameter OrderTraceVO orderTraceVO) {
        SessionUser sessionUser = new SessionUser();
        if (null == sessionUser.get()) throw new BizException("未获取到登录信息!");

        if (!sessionUser.isPm()) {
            return SingleResponse.buildFailure("-1","无权访问该接口");
        }
        if(null == orderTraceVO||StringUtils.isBlank(orderTraceVO.getId())){
            return SingleResponse.buildFailure("-1","参数有误");
        }
        String[] traceIdsArr = StringUtils.split(orderTraceVO.getId(), ",");
        if (null == traceIdsArr || traceIdsArr.length <= 0) {
            return SingleResponse.buildFailure("-1","参数有误");
        }

        List<String> traceIdList = new ArrayList<>();
        for (String traceId : traceIdsArr) {
            traceIdList.add(traceId);
        }
        if (CollectionUtils.isEmpty(traceIdList)) {
            return SingleResponse.buildFailure("-1","参数有误");
        }
        yunShuService.pushOrderToInvoicingListener(traceIdList);
        return SingleResponse.of("成功");
    }
}
